package com.tsingteng.cosfun.ui.message;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BasePresenterFragment;
import com.tsingteng.cosfun.bean.NotificationBean;
import com.tsingteng.cosfun.common.Navigate;
import com.tsingteng.cosfun.utils.AppUtils;
import com.tsingteng.cosfun.utils.DBUtils;
import com.tsingteng.cosfun.utils.SPUtils;
import com.tsingteng.cosfun.utils.TimeUtils;
import com.tsingteng.cosfun.widget.MyRecycleView;
import com.tsingteng.cosfun.widget.TitleView;


import java.util.Objects;

import butterknife.BindView;


/**
 * 描述: 消息 fragment
 * 创建人: 肖利
 * 时间: 2018/7/30
 * qq: (872179966)
 */

public class MessageFragment extends BasePresenterFragment<MessagePresenter, MessageContract.IMessageView> implements MessageContract.IMessageView,View.OnClickListener, TitleView.OnTitleClickListener{


    @BindView(R.id.tv_view)
    TitleView tvView;
    @BindView(R.id.rv_message_rv)
    MyRecycleView rvMessageRv;
    private String user_type = "MESSAGE";
    private int page = 1;
    private TextView tv_fans_count;
    private TextView tv_comment_count;
    private TextView tv_at_count;
    private TextView tv_great_count;
    private int systemCount;
    private int officalCount;

    @Override
    protected void getBundle(Bundle bundle) {
    }

    @Override
    protected void initData(View rootView) {
        tvView.setIvLeft(R.drawable.back_big_icon);
        tvView.setOnTitleClickListener(this);
        mPresenter.loginUserNotification(user_type,page);
        initReadCount();
    }

    @Override
    protected MessagePresenter createPresenter() {
        return new MessagePresenter();
    }

    @Override
    protected View initHeader() {
        View headerView =  View.inflate(getContext(), R.layout.view_message_header, null);
        LinearLayout mLlFans = headerView.findViewById(R.id.ll_fans);
        LinearLayout mLlGreat = headerView.findViewById(R.id.ll_great);
        LinearLayout mLlComment = headerView.findViewById(R.id.ll_comment);
        LinearLayout mLlAtMe = headerView.findViewById(R.id.ll_at_me);
        LinearLayout mLlPlay = headerView.findViewById(R.id.ll_play);
        tv_fans_count = headerView.findViewById(R.id.tv_fans_count);
        tv_comment_count = headerView.findViewById(R.id.tv_comment_count);
        tv_at_count = headerView.findViewById(R.id.tv_at_count);
        tv_great_count = headerView.findViewById(R.id.tv_great_count);
        mLlFans.setOnClickListener(this);
        mLlGreat.setOnClickListener(this);
        mLlComment.setOnClickListener(this);
        mLlAtMe.setOnClickListener(this);
        mLlPlay.setOnClickListener(this);
        return headerView;
    }


    private void initReadCount(){
        int fansCount = AppUtils.getMessageNumber("FANS");
        int pointCount = AppUtils.getMessageNumber("POINT");
        int commentCount = AppUtils.getMessageNumber("REVIEW");
        int greatCount = AppUtils.getMessageNumber("FAVOR");
        systemCount = AppUtils.getMessageNumber("NOTICE");
        officalCount = AppUtils.getMessageNumber("HELPER");

        if(fansCount == 0){
            tv_fans_count.setVisibility(View.INVISIBLE);
        }else if(fansCount > 99){
            tv_fans_count.setVisibility(View.VISIBLE);
            tv_fans_count.setText(getResources().getString(R.string.message_num));
        }else{
            tv_fans_count.setVisibility(View.VISIBLE);
            tv_fans_count.setText(String.valueOf(fansCount));
        }
        if(pointCount == 0){
            tv_at_count.setVisibility(View.INVISIBLE);
        }else if(pointCount > 99){
            tv_at_count.setVisibility(View.VISIBLE);
            tv_at_count.setText(getResources().getString(R.string.message_num));
        }else{
            tv_at_count.setVisibility(View.VISIBLE);
            tv_at_count.setText(String.valueOf(pointCount));
        }
        if(commentCount == 0){
            tv_comment_count.setVisibility(View.INVISIBLE);
        }else if(commentCount > 99){
            tv_comment_count.setVisibility(View.VISIBLE);
            tv_comment_count.setText(getResources().getString(R.string.message_num));
        }else{
            tv_comment_count.setVisibility(View.VISIBLE);
            tv_comment_count.setText(String.valueOf(commentCount));
        }
        if(greatCount == 0){
            tv_great_count.setVisibility(View.INVISIBLE);
        }else if(greatCount > 99){
            tv_great_count.setVisibility(View.VISIBLE);
            tv_great_count.setText(getResources().getString(R.string.message_num));
        }else{
            tv_great_count.setVisibility(View.VISIBLE);
            tv_great_count.setText(String.valueOf(greatCount));
        }
//        tv_great_count.setText(String.valueOf(greatCount));
//        tv_at_count.setText(String.valueOf(pointCount));
//        tv_comment_count.setText(String.valueOf(commentCount));
    }

    @Override
    protected MyRecycleView createRecycleView() {
        return rvMessageRv;
    }

    @Override
    protected MessageAdapter createAdapter() {
        return new MessageAdapter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_message;
    }

    @SuppressLint("ResourceType")
    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        switch (position) {
            case 0:
                Navigate.startSystemNotification(getContext());
                AppUtils.setMessageNumber("NOTICE",0);
                TextView countNotifi = view.findViewById(R.id.tv_message_item_count);
                countNotifi.setVisibility(View.INVISIBLE);
                systemCount = 0;
                return;
            case 1:
                Navigate.startOfficalNotification(getContext());
                TextView countOffica = view.findViewById(R.id.tv_message_item_count);
                countOffica.setVisibility(View.INVISIBLE);
                officalCount = 0;
                return;
            default:
                int id;
                NotificationBean.ListBean listBean = (NotificationBean.ListBean) mAdapter.getData().get(position);
                int user_id = listBean.getSource().getId();
                String title;
                if(SPUtils.init().getInt("profile_id") == user_id){
                    id = listBean.getTarget().getId();
                    title = listBean.getTarget().getNickName();
                }else{
                    id = user_id;
                    title = listBean.getSource().getNickName();
                }
                Navigate.startMessageNotification(getContext(), id, title);
                TextView count = view.findViewById(R.id.tv_message_item_count);
                count.setVisibility(View.INVISIBLE);
                return;
        }
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        mPresenter.loginUserNotification(user_type,page);
    }


    @Override
    protected void refresh() {
        page = 1;
        mPresenter.loginUserNotification(user_type,page);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_fans:
                Navigate.startCommont(getContext(),CommonActivity.KEY_FANS);
                tv_fans_count.setVisibility(View.GONE);
                break;
            case R.id.ll_great:
                Navigate.startCommont(getContext(),CommonActivity.KEY_GREAT,"FAVOR");
                tv_great_count.setVisibility(View.GONE);
                break;
            case R.id.ll_comment:
                Navigate.startCommont(getContext(),CommonActivity.KEY_COMMENT,"REVIEW");
                tv_comment_count.setVisibility(View.GONE);
                break;
            case R.id.ll_at_me:
                Navigate.startCommont(getContext(),CommonActivity.KEY_AT_ME,"POINT");
                tv_at_count.setVisibility(View.GONE);
                break;
            case R.id.ll_play:
                Navigate.startCommont(getContext(),CommonActivity.KEY_PLAY);
                break;

        }
    }

    @Override
    public void getShowUserData(NotificationBean notificationBean) {
        if (page==1) {
            mAdapter.addData(0,new NotificationBean.ListBean("系统通知最新数据","NOTICE",15210525012L,0,0,new NotificationBean.ListBean.SourceBean(TimeUtils.getCurrentDate() + "", 0, R.drawable.news_notice_icon, "系统通知", 2, null),0,new NotificationBean.ListBean.TargetBean(TimeUtils.getCurrentDate()+"",0, R.drawable.news_notice_icon, "系统通知", 2,null),"NOTICE",systemCount,15148046642L));
            mAdapter.addData(1,new NotificationBean.ListBean("官方助手最新数据","HELPER",15210525012L,1,0,new NotificationBean.ListBean.SourceBean(TimeUtils.getCurrentDate()+"",1, R.drawable.news_logo_icon, "官方助手", 2,null),0,new NotificationBean.ListBean.TargetBean(TimeUtils.getCurrentDate()+"",1, R.drawable.news_logo_icon, "官方助手", 2,null),"HELPER",0,15148046642L));
        }
        if (rvMessageRv!= null) rvMessageRv.refreshComplete();
        if (notificationBean==null || notificationBean.getList()==null) return;
        mAdapter.addData(notificationBean.getList());
        mAdapter.notifyDataSetChanged();
        if (notificationBean.isHasNextPage()) {
            mAdapter.loadMoreComplete();
        }else {
            mAdapter.loadMoreEnd();
        }
    }

    @Override
    public void onLeft() {
        Objects.requireNonNull(getActivity()).finish();
    }

    @Override
    public void onRight() {

    }

    @Override
    public void onRight2() {

    }
}
